package com.tjnu.baiyi.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tjnu.baiyi.entity.TbAlbum;
import com.tjnu.baiyi.entity.TbAlbumType;
import com.tjnu.baiyi.entity.TbFriend;
import com.tjnu.baiyi.entity.TbMessage;
import com.tjnu.baiyi.entity.TbUser;
import com.tjnu.baiyi.entity.TbVisitor;

/**
 * 用户Action
 * 
 * @author DELL
 * 
 */
@Scope("prototype")
@Controller("tbUserAction")
public class TbUserAction extends BaseAction {
	private static final long serialVersionUID = 659132375865544670L;

	/**
	 * 登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		try {
			System.out.println("TbUserAction.login()");
			if (user != null) {
				System.out.println("email:" + user.getEmail() + " ,password:"
						+ user.getPassword());
				TbUser currentUser = tbUserDao.login(user.getEmail(),
						user.getPassword());// 验证用户邮箱和密码
				if (currentUser != null) {// 验证通过：把用户信息存入session中
					List<TbUser> list = tbUserDao.findAll();// 根据用户的人气值从高到低进行排序查询
					List<TbAlbum> list2 = tbAlbumDao.findAllByPage(0);// 根据相册的热度从高到低进行查询
					Integer size = tbAlbumDao.getfindAllSize();// 获取所有的相册数
					boolean flag = tbUserDao.isFirstUserHotAndCurrentUser(list
							.get(0).getUserId(), currentUser.getUserId());// 判断排行最高的用户和当前登录用户是否为好友，true：是；false：不是
					if (flag) {
						session.setAttribute("flag", 0);
					} else {
						session.setAttribute("flag", 1);
					}
					session.setAttribute("currentUser", currentUser);// 将用户信息保存在Session对象中
					session.setAttribute("userHot", list);
					session.setAttribute("albumHot", list2);
					session.setAttribute("size", size / 10);// 所有的页数，没有设置显示10条数据记录
					System.out.println("return success");
					return SUCCESS;
				} else {// 验证不通过：返回错误提示信息
					System.out.println("user not exist");
					return LOGIN;
				}
			} else {
				System.out.println("user == null");
				return LOGIN;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("login fail");
			e.printStackTrace();
			return LOGIN;
		}
	}

	/**
	 * 退出||注销
	 * 
	 * @return
	 * @throws Exception
	 */
	public String exit() throws Exception {
		session.removeAttribute("currentUser");// 把当前登录的用户信息移除
		// 退出成功，返回“登录”页面
		return LOGIN;
	}

	/**
	 * 注册
	 * 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		try {
			System.out.println("TbUserAction.register()");
			if (user != null) {
				user.setUserHot(0);// 设置人气为0
				user.setQzoneName(user.getUserName() + "的空间");// 设置用户的空间名称
			} else {
				return "register";// 用户不存在，重返“注册”页面
			}
			tbUserDao.save(user);
			// 注册成功，返回登录页面
			return LOGIN;
		} catch (Exception e) {
			return "error";
		}
	}

	/**
	 * 前往register前获取所有的邮箱信息
	 * 
	 * @return
	 */
	public String getAllEmail() throws Exception {
		List<String> email = tbUserDao.getAllEmailInTbUserTable();
		if (email.size() != 0) {
			session.setAttribute("Email", email);
		} else {
			session.setAttribute("Email", "");
		}
		return SUCCESS;
	}

	/**
	 * 根据用户id获取用户的具体信息//该用户并不是当前登录的用户
	 * 
	 * @return
	 */
	public String userDetail() throws Exception {
		String id = request.getParameter("id").toString();
		TbUser tbUser = tbUserDao.findById(Integer.parseInt(id));
		session.setAttribute("user", tbUser);
		return SUCCESS;
	}

	/**
	 * 前往“我的主页”
	 * 
	 * @return
	 */
	public String toMyHome() throws Exception {
		System.out.println("TbUserAction.toMyHome()");
		// 获取当前登录的用户：评论需要用到该用户的id
		TbUser user1 = (TbUser) session.getAttribute("currentUser");
		// 获取所有相册类型
		 List<TbAlbumType> all_type = tbAlbumTypeDao.findAll();
		List<TbAlbumType> type = tbAlbumTypeDao.findAllByUserId(user1
				.getUserId());
		if (type != null) {
			System.out.println("type size:"+type.size());
			for (int i = 0; i < type.size(); i++) {
				System.out.println("type name:" + type.get(i));
				System.out.println("type name:" + type.get(i).getAlbumTypeId());
			}
		}
		// 获取我的所有相册(根据相册分类的先后进行排序)
		List<TbAlbum> albums = tbAlbumDao.findAllMyAlbumBytbAlbumTypeId(user1
				.getUserId());
		// 获取所有的访客记录
		List<TbVisitor> visitor = tbVisitorDao.findAllByCurrentUserId(user1
				.getUserId());
		session.setAttribute("all_type", all_type);
		session.setAttribute("types", type);
		session.setAttribute("myAlbum", albums);
		session.setAttribute("myVisitor", visitor);
		return SUCCESS;
	}

	/**
	 * 前往他人主页
	 * 
	 * @return
	 */
	public String toOtherHome() throws Exception {
		System.out.println("TbUserAction.toOtherHome()");
		// 获取当前登录的用户信息
		TbUser currentUser = (TbUser) session.getAttribute("currentUser");
		// 获取从前台传过来的用户的id
		String id = request.getParameter("id");
		TbUser user1 = tbUserDao.findById(Integer.parseInt(id));
		tbUserDao.updateVisitor(currentUser.getUserId(), Integer.parseInt(id),
				user1.getVisitorSize());// 更新访问该用户空间的访客记录
		// 获取所有相册类型
		 List<TbAlbumType> all_type = tbAlbumTypeDao.findAll();
		List<TbAlbumType> type = tbAlbumTypeDao.findAllByUserId(Integer
				.parseInt(id));
		// 获取我的所有相册(根据相册分类的先后进行排序)
		List<TbAlbum> albums = tbAlbumDao.findAllMyAlbumBytbAlbumTypeId(user1
				.getUserId());
		// 获取所有的访客记录
		List<TbVisitor> visitor = tbVisitorDao.findAllByCurrentUserId(user1
				.getUserId());
		// 判断登录的用户是否“赞”过从前台传过来的用户

		// 判断登录的用户是否“关注”过从前台传过来的用户
		Boolean validateFriend = tbFriendDao.validateFriendByCurrentId(
				Integer.parseInt(id), user1.getUserId());
		if (validateFriend) {
			session.setAttribute("validateFriend", 1);// 已经关注过咯，设置为1，用于在页面中进行判断
		} else {
			session.setAttribute("validateFriend", 0);// 还未关注
		}
		session.setAttribute("user", user1);
		session.setAttribute("all_type", all_type);
		session.setAttribute("types", type);
		session.setAttribute("myAlbum", albums);
		session.setAttribute("myVisitor", visitor);
		return SUCCESS;
	}

	/**
	 * 前往“我的留言板”
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toMyMessageBoard() throws Exception {
		// 获取当前登录的用户信息
		TbUser currentUser = (TbUser) session.getAttribute("currentUser");
		List<TbMessage> list = tbMessageDao.findAllByUserId(currentUser
				.getUserId());
		session.setAttribute("messages", list);
		return SUCCESS;
	}

	/**
	 * 前往“他人的留言板”
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toOtherMessageBoard() throws Exception {
		String id = request.getParameter("id").toString();// 获取该留言板的用户id
		TbUser user1 = tbUserDao.findById(Integer.parseInt(id));// 获取该留言板的用户信息
		List<TbMessage> list = tbMessageDao.findAllByUserId(Integer
				.parseInt(id));
		session.setAttribute("messages", list);
		session.setAttribute("user", user1);
		return SUCCESS;
	}

	/**
	 * 添加“关注”记录，及添加好友
	 * 
	 * @throws Exception
	 */
	/*
	 * public String addFriend() throws Exception { try { // 获取当前登录的用户信息 TbUser
	 * currentUser = (TbUser) session.getAttribute("currentUser"); // 获取要关注的用户Id
	 * String id = request.getParameter("id").toString();
	 * System.out.println("id:" + id); // 先判断是否已经关注过该好友 boolean flag =
	 * tbFriendDao.validateFriendByCurrentId( Integer.parseInt(id),
	 * currentUser.getUserId()); if (flag) { // 未关注过：进行关注 TbUser friend =
	 * tbUserDao.findById(Integer.parseInt(id)); TbFriend myFriend = new
	 * TbFriend(); myFriend.setTbUserByUserId(currentUser);
	 * myFriend.setTbUserByFriendId(friend); tbFriendDao.save(myFriend); } else
	 * { System.out.println("flag == false"); // 关注过：什么也不做 } return SUCCESS; }
	 * catch (Exception e) { return ERROR; } }
	 */

	/**
	 * 前往“关注的人”页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toFriend() throws Exception {
		try {
			// 获取当前登录的用户信息
			TbUser currentUser = (TbUser) session.getAttribute("currentUser");
			// 获取要关注的用户Id
			List<TbFriend> myFriends = tbFriendDao.findAllByUserId(currentUser
					.getUserId());// 我关注的用户
			List<TbFriend> list = tbFriendDao.findAllByFriendId(currentUser
					.getUserId());// 关注我的用户
			for (int i = 0; i < myFriends.size(); i++) {
				System.out.println("id:" + myFriends.get(i).getId()
						+ " ,tbUserByUserId:"
						+ myFriends.get(i).getTbUserByUserId()
						+ " ,tbUserByFriendId:"
						+ myFriends.get(i).getTbUserByFriendId());
			}
			session.setAttribute("list", list);
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}
}
